package com.orange.core.service.merchantReport.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orange.core.service.merchantReport.MerchantReportService;
import com.orange.core.mapper.merchantReport.MerchantReportMapper;
import com.orange.core.pojo.PageReqDto;
import com.orange.core.pojo.entity.merchantReport.MerchantReport;
import com.orange.core.pojo.entity.merchantReport.dto.MerchantReportDTO;
import com.orange.core.pojo.entity.merchantReport.vo.MerchantReportVO;
import com.orange.core.pojo.req.merchantReport.MerchantReportPageReq;
import com.orange.core.util.ConvertUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * @author lwm
 * @description 针对表【merchant_report(商户报表表)】的数据库操作Service实现
 * @createDate 2025-05-29 23:52:06
 */
@Service
public class MerchantReportServiceImpl extends ServiceImpl<MerchantReportMapper, MerchantReport>
        implements MerchantReportService {

    @Override
    public Page<MerchantReportVO> page(PageReqDto<MerchantReportPageReq> pageReqDto) {
        String merchantName = Optional.ofNullable(pageReqDto.getCondition())
                .map(MerchantReportPageReq::getMerchantName).orElse(null);
        Page<MerchantReport> page = this.lambdaQuery()
                .like(StrUtil.isNotBlank(merchantName), MerchantReport::getMerchantName, merchantName)
                .orderByDesc(MerchantReport::getId)
                .page(pageReqDto.toPage());

        Page<MerchantReportVO> merchantReportVOPage = new Page<>(pageReqDto.getPage(), pageReqDto.getSize(), page.getTotal());
        //列表转VO
        List<MerchantReportVO> merchantReportVOList = ConvertUtils
                .convertListToVo(page.getRecords(), MerchantReportVO.class);

        merchantReportVOPage.setRecords(merchantReportVOList);


        return merchantReportVOPage;
    }

    @Override
    public MerchantReportVO info(int id) {
        MerchantReport merchantReport = this.getById(id);
        Assert.notNull(merchantReport, "商户报表不存在");
        MerchantReportVO merchantReportVO = new MerchantReportVO();
        BeanUtil.copyProperties(merchantReport, merchantReportVO);
        return merchantReportVO;
    }

    @Override
    @Transactional
    public boolean save(MerchantReportDTO merchantReportDTO) {
        MerchantReport merchantReport = new MerchantReport();
        BeanUtil.copyProperties(merchantReportDTO, merchantReport);
        boolean result = this.save(merchantReport);
        return result;
    }

    @Override
    @Transactional
    public boolean update(MerchantReportDTO merchantReportDTO) {
        MerchantReport merchantReport = new MerchantReport();
        BeanUtil.copyProperties(merchantReportDTO, merchantReport);
        boolean result = this.updateById(merchantReport);
        return result;
    }

    @Override
    @Transactional
    public boolean deleteMerchantReportByIds(Integer[] ids) {
        boolean result = this.removeByIds(Arrays.asList(ids));
        return result;
    }

    @Override
    public String downloadFile(Integer id) {
        MerchantReport merchantReport = this.getById(id);
        Assert.notNull(merchantReport, "商户报表不存在");
        String filePath = merchantReport.getFilePath();
        Assert.notNull(filePath, "文件路径不存在");
        return filePath;
    }
}




